package com.jcnet.bizaia.tools;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

public class BBTools {

	public static final int  ADRESS_RESULT_CODE = 0x10;
	public static final int  PHOTO_RESULT_CODE = 0x20;

	public static final int  TALK_LIST_RESULT_CODE = 0x30;
	public static final int  DISCOUNT_RESULT_CODE = 0x40;

	
	 public static void setListViewHeightBasedOnChildren(ListView listView) {
         ListAdapter listAdapter = listView.getAdapter(); 
         if (listAdapter == null) {
             // pre-condition
             return;
         }

         int totalHeight = 0;
         for (int i = 0; i < listAdapter.getCount(); i++) {
             View listItem = listAdapter.getView(i, null, listView);
             listItem.measure(0, 0);
             totalHeight += listItem.getMeasuredHeight();
         }

         ViewGroup.LayoutParams params = listView.getLayoutParams();
         params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
         listView.setLayoutParams(params);
     }
	/** 
	 * MD5 加密 
	 */  
	public static String getMD5Str(String str) {  
		MessageDigest messageDigest = null;  

		try {  
			messageDigest = MessageDigest.getInstance("MD5");  

			messageDigest.reset();  

			messageDigest.update(str.getBytes("UTF-8"));  
		} catch (NoSuchAlgorithmException e) {  
			//System.out.println("NoSuchAlgorithmException caught!");  
			////System.exit(-1);  
		} catch (UnsupportedEncodingException e) {  
			e.printStackTrace();  
		}  

		byte[] byteArray = messageDigest.digest();  

		StringBuffer md5StrBuff = new StringBuffer();  

		for (int i = 0; i < byteArray.length; i++) {              
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)  
				md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));  
			else  
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));  
		}  

		return md5StrBuff.toString();  
	} 

	/**
	 * 判断字符串是否为空
	 * @param string
	 * @return
	 */
	public static boolean isEmptyString(String string) {
		// TODO Auto-generated method stub
		
		return string!=null&&string.length()!=0;
	}


	public static String getSDPath(){ 
		File sdDir = null; 
		boolean sdCardExist = Environment.getExternalStorageState() 
				.equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在 
		if (sdCardExist) 
		{ 
			sdDir = Environment.getExternalStorageDirectory();//获取跟目录 
		}else{
			return null;
		}
		return sdDir.toString(); 

	} 

	
	// 删除文件

	public static void deleteFile(File file) {
		if (file.exists()) { // 判断文件是否存在
			if (file.isFile()) { // 判断是否是文件
				file.delete(); // delete()方法 你应该知道 是删除的意思;
			} else if (file.isDirectory()) { // 否则如果它是一个目录
				File files[] = file.listFiles(); // 声明目录下所有的文件 files[];
				for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件
					deleteFile(files[i]); // 把每个文件 用这个方法进行迭代
				}
			}
			file.delete();
		} else {
			//System.out.println("文件不存在");
		}
	}

	
	//检查文件是否存在
	
	public static boolean isFileExit(String path){
        if(path == null){
            return false;
        }
        try{
            File f = new File(path);
            if(!f.exists()){
                return false;
            }
        }catch (Exception e) {
            // TODO: handle exception
        }
        return true;
    }
	

    
    
    
    public static boolean isTabletDevice(Context context) {
    	TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    	String phoneNum =  telManager.getLine1Number() ;
    	if(TextUtils.isEmpty(phoneNum)){
    		return true;
    	}else{
    		return false;
    	}
    }
    
    
    public static String getString(int r,Context context){
    	
    	return context.getResources().getString(r);
    }
}
